package com.example.librarymanagementsystem.config;

import com.example.librarymanagementsystem.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ResponseBody
@ControllerAdvice
@Slf4j
public class ExceptionAdvice {
    /**
     * 统一异常处理
     * @param e
     * @return
     */
    @ExceptionHandler
    public Result handleException(Exception e){
        //建议打印日志
        log.error("e: "+e);
        return Result.fail("发生内部错误");
    }

    @ExceptionHandler(NullPointerException.class)
    public Result handleException2(Exception e){
        log.error("e: ",e);
        return Result.fail("空指针异常");
    }

    @ExceptionHandler
    public Result handleException(ArithmeticException e){
        log.error("e: ",e);
        return Result.fail("算术异常");
    }
}
